<?php
class Core_Frontend_Component_Customer_Controller_Showroom extends Core_Frontend_Share_Component_Controller {

    public function sdAction($id){
    	$model = App::getModel('customer/showroom')->load($id);
        if (!$model->getId() || $model->getStatus() != 'published') {
        	return $this->errorResult('记录存在或已删除');
        }
    
		$model->updateClickCount();
        $content = $this->loadEbook($model->getSdUrl());
        echo $content;
        die();       
    }
    
    public function hdAction($id) {
        $model = App::getModel('customer/showroom')->load($id);
        if (!$model->getId() || $model->getStatus() != 'published') {
        	return $this->errorResult('记录存在或已删除');
        }
        return $this->templateResult(array('model'=>$model));
    }
    
    public function hdPostAction($showroomid, $code){
    	$model = App::getModel('customer/showroom')->load($showroomid);
    	$certificate = App::getModel('customer/showroom_certificate')->loadByCode($model->getId(), $code);
    	
    	if(!$certificate->getId()){
    		return $this->redirectResult($this->getUrl(array('id'=>$model->getId())), '认证码错误');
    	}
        
        $expiryDate = $certificate->getExpiryDate();
        if(strlen($expiryDate) > 0){
        	$now = time();
        	$expiry = strtotime($expiryDate);
        	if($expiry < $now){
        		return $this->errorResult('拒绝访问');
        	}
        }
        	
    	$accessTimes = $certificate->getAccessTimes();
        if (strlen($accessTimes) > 0) {
        	if($accessTimes == 0){
        		return $this->errorResult('拒绝访问');
        	}else{
        		$certificate->updateAccessTimes();
        	}
        }    
        
		$model->updateClickCount();
		$certificate->updateClickCount();
        $content = $this->loadEbook($model->getHdUrl());
        echo $content;
        die();    	
    }

    public function proxyAction() {
        $method = $this->getRequest()->getMethod();
        $url = base64_decode($this->getRequest()->getParameter('proxyurl'));
        $args = $this->getRequest()->getAllForm();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);
        }
        curl_setopt($ch, CURLOPT_HEADER, 0);
        if (!empty($args)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
        }
//        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type'=>'application/x-www-form-urlencoded'));
        curl_exec($ch);
        $content = ob_get_clean();
        curl_close($ch);
        header('Content-Type:text/xml');
        echo $content;
        die();
    }

    private function loadEbook($url) {
        $urlInfo = parse_url($url);
        $ebookUrl = 'http://' . $urlInfo['host'];
        if (isset($urlInfo['port']) && $urlInfo['port'] != '80') {
            $ebookUrl .= ':' . $urlInfo['port'];
        }
        $ebookUrl .= str_replace('ebook.php', '', $urlInfo['path']);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        ob_start();
        curl_exec($ch);
        $content = ob_get_clean();
        curl_close($ch);
        $myhtml = '<script language="javascript" type="text/javascript" src="http://' . $this->getRequest()->getHostName() . '/pub/share/js/jquery/jquery.js"></script>';
        $myhtml .= '<script language="javascript" type="text/javascript" src="http://' . $this->getRequest()->getHostName() . '/pub/skin/stable/frontend/js/ebook.js"></script>';
        $content = str_replace('<head>', '<head><base href="' . $ebookUrl . '"/>', $content);
        $content = str_replace('../../../include/viewAudioObjectXml.php', base64_encode('http://www.cmags.cn:88/ocp/include/viewAudioObjectXml.php'), $content);
        $content = str_replace('../../../include/viewHyperLinkXml.php', base64_encode('http://www.cmags.cn:88/ocp/include/viewHyperLinkXml.php'), $content);
        $content = str_replace('../../../include/viewWordIndexXml.php', base64_encode('http://www.cmags.cn:88/ocp/include/viewWordIndexXml.php'), $content);
        $content = str_replace('../../../include/viewBookXmml.php', base64_encode('http://www.cmags.cn:88/ocp/include/viewBookXmml.php'), $content);
//        $content = str_replace('../../../script/ajax.js', 'http://beta.fscsddt.com/pub/skin/stable/frontend/js/ebookajax.js', $content);
        $content = str_replace('</body>', $myhtml . '</body>', $content);
        return $content;
    }   
}

